﻿/*
==================================================================
    注意：此文件为Auto代码，不可手动修改【用户与角色】
==================================================================
*/

namespace Services
{
    /// <summary>
    /// Auto用户与角色
    /// </summary>
    public abstract class AutoUserRoleMapping : BaseMapping<UserRole, UserRoleModel, UserRoleFilterModel>
    {
        public AutoUserRoleMapping(ICurrentUser currentUser)
            : base(currentUser) { }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override UserRoleModel Entity2Model(UserRole entity)
        {
            UserRoleModel model = new UserRoleModel();

            model.UserRoleID = entity.UserRoleID;
            model.GroupInfoID = entity.GroupInfoID;
            model.UserID = entity.UserID;
            model.RoleID = entity.RoleID;

            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override UserRole Model2Entity(UserRoleModel model)
        {
            UserRole entity = new UserRole();

            entity.UserRoleID = model.UserRoleID;
            entity.GroupInfoID = model.GroupInfoID;
            entity.UserID = model.UserID;
            entity.RoleID = model.RoleID;

            return entity;
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<UserRole> FilterModel2Where(UserRoleFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //编号
            if (filterModel._UserRoleID.HasValue)
            {
                where = where.And(x => x.UserRoleID == filterModel._UserRoleID.Value);
            }
            if (filterModel._UserRoleIDs.IsNotEmpty() && filterModel._UserRoleIDs.Count > 0)
            {
                where = where.And(x => filterModel._UserRoleIDs.Contains(x.UserRoleID));
            }
            if (filterModel._NotUserRoleIDs.IsNotEmpty() && filterModel._NotUserRoleIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotUserRoleIDs.Contains(x.UserRoleID));
            }

            //集团
            if (filterModel._GroupInfoID.HasValue)
            {
                where = where.And(x => x.GroupInfoID == filterModel._GroupInfoID.Value);
            }
            if (filterModel._GroupInfoIDs.IsNotEmpty() && filterModel._GroupInfoIDs.Count > 0)
            {
                where = where.And(x => filterModel._GroupInfoIDs.Contains(x.GroupInfoID));
            }
            if (filterModel._NotGroupInfoIDs.IsNotEmpty() && filterModel._NotGroupInfoIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotGroupInfoIDs.Contains(x.GroupInfoID));
            }

            //用户
            if (filterModel._UserID.HasValue)
            {
                where = where.And(x => x.UserID == filterModel._UserID.Value);
            }
            if (filterModel._UserIDs.IsNotEmpty() && filterModel._UserIDs.Count > 0)
            {
                where = where.And(x => filterModel._UserIDs.Contains(x.UserID));
            }
            if (filterModel._NotUserIDs.IsNotEmpty() && filterModel._NotUserIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotUserIDs.Contains(x.UserID));
            }

            //用户角色
            if (filterModel._RoleID.HasValue)
            {
                where = where.And(x => x.RoleID == filterModel._RoleID.Value);
            }
            if (filterModel._RoleIDs.IsNotEmpty() && filterModel._RoleIDs.Count > 0)
            {
                where = where.And(x => filterModel._RoleIDs.Contains(x.RoleID));
            }
            if (filterModel._NotRoleIDs.IsNotEmpty() && filterModel._NotRoleIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotRoleIDs.Contains(x.RoleID));
            }

            return where;
        }
        #endregion
    }
}

